<template>
  <a-modal
    :title="currentRow ? '编辑' : '新增'"
    :visible="dialogVisible"
    okText="确认"
    cancelText="取消"
    :width="620"
    :confirmLoading="loading"
    @ok="handleSure"
    @cancel="
      () => {
        $emit('cancel');
      }
    "
  >
    <a-form-model
      :model="fileFrom"
      :label-col="{ span: 4 }"
      :wrapper-col="{ span: 16 }"
      ref="userFrom"
      hideRequiredMark
    >
      <a-form-model-item prop="id" label="id" v-show="currentRow">
        <a-input v-model="fileFrom.id" disabled />
      </a-form-model-item>
      <a-form-model-item prop="fileName" label="分享链接" hasFeedback>
        <a-textarea v-model="fileFrom.shareLink" :autoSize="{ minRows: 3, maxRows: 5 }" />
      </a-form-model-item>
      <a-form-model-item prop="shareLock" label="需要提取码">
        <a-switch v-model="fileFrom.shareLock">
          <a-icon slot="checkedChildren" type="check" />
          <a-icon slot="unCheckedChildren" type="close" />
        </a-switch>
      </a-form-model-item>
      <a-form-model-item prop="fileType" label="提取码" hasFeedback>
        <a-input v-model="fileFrom.shareCode" :disabled="!fileFrom.shareLock" />
      </a-form-model-item>
      <a-form-model-item prop="filePath" label="创建时间" hasFeedback>
        <a-date-picker placeholder="选择创建时间" v-model="fileFrom.createTime" />
      </a-form-model-item>
      <a-form-model-item prop="filePath" label="过期时间" hasFeedback>
        <a-date-picker placeholder="选择过期时间" v-model="fileFrom.expirationTime" />
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
import { editTable, addTable } from '@/api/shareManage';
import standardTree from '@/components/standardTree/index';
export default {
  name: 'shareModel',
  props: {
    currentRow: [Object, null],
    dialogVisible: Boolean,
    roleOption: Array
  },
  components: { standardTree },
  data() {
    return {
      fileFrom: {
        role: ''
      },
      loading: false
    };
  },

  mounted() {
    this.fileFrom = { ...this.currentRow } || {
      role: ''
    };
  },
  methods: {
    handleSure() {
      var formatDateTime = function(date) {
        date = new Date(date);
        const y = date.getFullYear();
        let m = date.getMonth() + 1;
        m = m < 10 ? '0' + m : m;
        let d = date.getDate();
        d = d < 10 ? '0' + d : d;
        let h = date.getHours();
        h = h < 10 ? '0' + h : h;
        let minute = date.getMinutes();
        minute = minute < 10 ? ("0" + minute) : minute;
        let second = date.getSeconds();
        second = second < 10 ? '0' + second : second;
        return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
      };

      this.loading = true;
      var { id, shareCode, shareLink, createTime, expirationTime, shareLock } = this.fileFrom;
      if (this.currentRow) {
        //编辑
        expirationTime = formatDateTime(expirationTime);
        createTime = formatDateTime(createTime);
        editTable({ id, shareCode, shareLink, createTime, expirationTime, shareLock }).then(() => {
          this.$message.success('修改成功!');
          this.loading = false;
          this.$emit('ok');
        });
      } else {
        //新增
        addTable({ id, shareCode, shareLink, createTime, expirationTime, shareLock }).then(() => {
          this.$message.success('添加成功!');
          this.loading = false;
          this.$emit('ok');
        });
      }
      this.loading = false;
    }
  }
};
</script>
